package com.ashin.type;

import com.ashin.bean.StringMulti;
import com.ashin.mapper.AccountMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 中英文翻译类型字段
 *
 * @Author: Ashin
 * @Date: 2020/9/3
 * @Description: com.juggernaut.type
 * @Version: 1.0.0
 */
@Component
@MappedTypes(StringMulti.class)
public class TranslateTypeHandler2 extends BaseTypeHandler<StringMulti> implements ApplicationContextAware {


    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, StringMulti parameter, JdbcType jdbcType) throws SQLException {
        System.out.println(i + ":" + parameter);
        System.out.println(applicationContext.getBean(AccountMapper.class).selectList(new QueryWrapper<>()));
        ps.setString(i, "^-^");
    }

    @Override
    public StringMulti getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String string = rs.getString(columnName);
        StringMulti stringMulti = new StringMulti();
        stringMulti.setName((string + "解密"));
        return stringMulti;
    }

    @Override
    public StringMulti getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String string = rs.getString(columnIndex);
        StringMulti stringMulti = new StringMulti();
        stringMulti.setName((string + "解密"));
        return stringMulti;
    }

    @Override
    public StringMulti getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String string = cs.getString(columnIndex);
        StringMulti stringMulti = new StringMulti();
        stringMulti.setName((string + "解密"));
        return stringMulti;
    }
}
